package com.lianxi.exacl;


import com.lianxi.model.CountInfo;
import com.lianxi.model.CountItem;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/excel")
public class ExaclController {

    @GetMapping("/test")
    public void excelPoiWeb(HttpServletResponse response) throws IOException {
        //文件名
        String filename = "";
        //最后返回的流
        InputStream input = TestExacl.test2();
        response.setHeader("Content-disposition", "attachment;filename=" + "workbook.xls");
        response.setHeader("Cache-Control", "no-cache");
    }

    @GetMapping("/test2")
    public void excelPoiWeb2(HttpServletResponse response) throws IOException {

        //当前用户桌面路径
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();

        response.setHeader("content-disposition", "attachment;filename="
                + URLEncoder.encode("省库rdb数据.xls", "UTF-8"));
        ServletOutputStream out = response.getOutputStream();

        List<String> sheetList = new ArrayList<>();
        sheetList.add("新闻信息导入量");
        sheetList.add("依法信息导入量");
        sheetList.add("领导信箱导入量");
        //表头
        List<String> datelist = new ArrayList<>();
        datelist.add("2022-1");
        datelist.add("2022-2");
        datelist.add("2022-3");
        datelist.add("2022-4");
        datelist.add("2022-5");
        //假数据
        List<CountItem> countList = new ArrayList<>();
        countList.add(new CountItem("2022-1", "20"));
        countList.add(new CountItem("2022-2", "30"));
        countList.add(new CountItem("2022-3", "40"));
        countList.add(new CountItem("2022-4", "50"));
        countList.add(new CountItem("2022-5", "50"));
        //假数据
        List<CountItem> countList2 = new ArrayList<>();
        countList2.add(new CountItem("2022-1", "50"));
        countList2.add(new CountItem("2022-2", "60"));
        countList2.add(new CountItem("2022-3", "40"));
        countList2.add(new CountItem("2022-4", "30"));
        countList2.add(new CountItem("2022-5", "50"));
        List<CountInfo> countInfoList = new ArrayList<>();
        CountInfo c1 = new CountInfo();
        c1.setSitename("济源市");
        c1.setCountItemList(countList);
        CountInfo c2 = new CountInfo();
        c2.setSitename("安阳市");
        c2.setCountItemList(countList2);
        countInfoList.add(c1);
        countInfoList.add(c2);
        HSSFWorkbook workbook = new HSSFWorkbook();
        for (String s : sheetList) {
            HSSFSheet sheet = workbook.createSheet(s);
            workbook = TestExacl.test4(workbook, sheet, countInfoList, datelist);
        }

        // 输出到本地
//        String excelName = "e:\\desktop\\省库rdb数据.xls";
//        FileOutputStream out = null;
        try {
//            out = new FileOutputStream(excelName);
            workbook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null)
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            out = null;
        }
    }


}
